<template>
  <el-form :model="form" label-width="100px">
    <el-form-item label="字典名称">
      <el-input v-model="form.dictName"></el-input>
    </el-form-item>
    <el-form-item label="字典描述">
      <el-input type="textarea" v-model="form.description"></el-input>
    </el-form-item>
    <el-form-item label="字典编码">
      <el-input v-model="form.dictCode"></el-input>
    </el-form-item>
    <el-form-item label="关联进件：" style="text-align: left">
      <el-select v-model="form.projectId" placeholder="请选择">
        <el-option
          v-for="item in findProjectList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button @click="close" v-no-more-click>取 消</el-button>
      <el-button type="primary" @click="onSubmit" v-no-more-click
        >确 定</el-button
      >
    </el-form-item>
  </el-form>
</template>

<script>
import { getFindProject } from "../../../src/assets/js/api";
export default {
  name: "OperaData",
  props: {
    codeId: {
      type: null,
      required: true,
    },
    dialogVisible: {
      type: Boolean,
      required: true,
    },
    dataValue: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      // eslint-disable-next-line vue/no-dupe-keys
      id: null,
      form: {
        id: null,
        dictName: "",
        description: "",
        dictCode: "",
        projectId: "",
      },
      findProjectList: [],
      find: [{ id: null, name: "不关联" }],
    };
  },
  created() {
    this.findProject();
    this.form.id = this.codeId;
  },
  methods: {
    //通过ID获取此字典的详情
    getItemCode(id) {
      this.axios({
        methods: "get",
        url: `sys/dict/findOne/${id}`,
      }).then((res) => {
        this.form = res.data.data;
      }),
        (err) => {
          console.log(err);
        };
    },
    //获取产品
    findProject() {
      if (this.id != null) {
        this.form = this.dataValue;
      }
      getFindProject().then((res) => {
        this.findProjectList = this.find.concat(res.data);
      });
    },
    //清空字符
    clearInput() {
      this.form = {
        id: "",
        dictName: "",
        description: "",
        dictCode: "",
      };
    },
    //提交保存
    onSubmit() {
      if (this.codeId == null) {
        this.axios({
          method: "post",
          url: "sys/dict/save",
          data: {
            codeId: this.form.codeId,
            dictName: this.form.dictName,
            description: this.form.description,
            dictCode: this.form.dictCode,
            projectId: this.form.projectId,
          },
        }).then(() => {
          this.$message({
            message: "恭喜你，创建成功一条消息",
            type: "success",
            offset: "100",
          });
          this.$emit("dialogVisible", false);
          this.clearInput();
        }),
          (err) => {
            console.log(err);
          };
      } else {
        this.axios({
          method: "put",
          url: "sys/dict/update",
          data: {
            id: this.form.id,
            dictName: this.form.dictName,
            description: this.form.description,
            dictCode: this.form.dictCode,
            projectId: this.form.projectId,
          },
        }).then(() => {
          this.$message({
            message: "恭喜你，成功修改一条消息",
            type: "success",
            offset: "100",
          });
          this.$emit("dialogVisible", false);
          this.clearInput();
        }),
          (err) => {
            console.log(err);
          };
      }
    },
    close() {
      this.$emit("dialogVisible", false);
      this.clearInput();
    },
  },
};
</script>
<style lang="less"></style>
